package com.zz.config;

import com.zz.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.boot.autoconfigure.jersey.JerseyProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Component
@Aspect
@Slf4j
public class LogAspect {

    /*
    声明切入点
     */
    @Pointcut("execution(public * com.zz.controller.*.*(..))")
    public void controllerMethod(){}

    /*
    在方法前执行
    前置通知
     */

    @Before("controllerMethod()")
    public void logReqInfo(JoinPoint joinPoint){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        log.info("请求地址url: {}", request.getRequestURL());
        log.info("请求方式：{} ", request.getMethod());
        log.info("请求ip地址： {}", request.getRemoteAddr());
        log.info("请求参数： {}", joinPoint.getArgs());
        log.info("请求方法： {}", joinPoint.getSignature().getDeclaringTypeName()+"  "+joinPoint.getSignature().getName());
    }

    /*
    在方法后执行
    后置通知
     */
    @AfterReturning(returning = "result",pointcut = "controllerMethod()")
    public void  logResInfo(Result result){
        log.info("响应结果： {}", result);
    }

}
